package com.example.foodsy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.foodsy.dto.AddressDTO;
import com.example.foodsy.dto.Result;
import com.example.foodsy.entity.RetaInfo;
import com.example.foodsy.entity.SlauInfo;
import com.example.foodsy.entity.WholInfo;
import com.example.foodsy.service.RetaInfoService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 零售商产品批号信息表 前端控制器
 * </p>
 *
 * @author cgc
 * @since 2025-09-23
 */
@RestController
@RequestMapping("/reta-info")
@CrossOrigin
public class RetaInfoController {
	@Autowired
	private RetaInfoService retaInfoService;
	//新增
	@PostMapping("addRetaInfo")
	public Result addRetaInfo(@RequestBody RetaInfo retaInfo){
		boolean save = retaInfoService.addRetaInfo(retaInfo);
		return save ? Result.ok():Result.fail("保存失败");
	}

	//listByState
	@GetMapping("/listByState/{state}/{retaId}")
	public Result listByState(@PathVariable Integer state,@PathVariable Integer retaId){
		return Result.ok(retaInfoService.list(new QueryWrapper<RetaInfo>().eq("state", state).eq("reta_id", retaId)));
	}

	////根据wbId查询上游企业信息
	@GetMapping("/getWholInfo/{wbId}")
	public Result getWholInfo(@PathVariable Integer wbId){
		AddressDTO<WholInfo> wholInfo = retaInfoService.getWholInfo(wbId);
		return Result.ok(wholInfo);
	}
	@GetMapping("getInfoBywbId/{wbId}")
	public Result getRetaInfo(@PathVariable String wbId){
		AddressDTO<WholInfo> addressDTO = retaInfoService.getRetaInfo(wbId);
		return Result.ok(addressDTO);

	}

	@GetMapping("/getRetaInfoByBatchId/{batchId}")
	public Result getRetaInfoByBatchId(@PathVariable String batchId){
		return Result.ok(retaInfoService.getOne(new QueryWrapper<RetaInfo>().eq("batch_id", batchId)));
	}

	@PostMapping("/editRetaInfo")
	public Result editRetaInfo(@RequestBody RetaInfo retaInfo){
		return retaInfoService.updateById(retaInfo) ? Result.ok() : Result.fail("修改失败");
	}

	@GetMapping("getRetaInfoBystate/{state}/{companyId}")
	public Result getRetaInfoBystate(@PathVariable String state, @PathVariable Integer companyId){
		List<RetaInfo> list = retaInfoService.getRetaInfoBystate( state, companyId);
		return Result.ok(list);
	}

	@PostMapping("/updateState/{batchId}")
	public Result updateState(@PathVariable String batchId){
		RetaInfo retaInfo = retaInfoService.getOne(new QueryWrapper<RetaInfo>().eq("batch_id", batchId));
		retaInfo.setState(3);
		return retaInfoService.updateById(retaInfo) ? Result.ok() : Result.fail("修改失败");
	}

	@GetMapping("/getInfoByQR/{qrCode}")
	public Result getInfoByQR(@PathVariable String qrCode){
		AddressDTO<RetaInfo> addressDTO  = retaInfoService.getInfoByQR(qrCode);
		return Result.ok(addressDTO);
	}

}
